package com.scale.scales.manager;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.text.TextUtils;

import com.scale.scales.bean.FoodBean;
import com.scales.scales.db.DaoMaster;
import com.scales.scales.db.DaoSession;
import com.scales.scales.db.FoodBeanDao;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

/**
 * Created by huangyin on 2018/8/7.
 */

public class DBManager {
    private static final String dbName = "kedacom_pad_db";
    private static DBManager mInstance;
    private DaoMaster.DevOpenHelper mOpenHelper;
    private Context mContext;

    public DBManager(Context context) {
        this.mContext = context;
        mOpenHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
    }

    /**
     * 获取单例引用
     *
     * @param context
     * @return
     */
    public static DBManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (DBManager.class) {
                if (mInstance == null) {
                    mInstance = new DBManager(context);
                }
            }
        }
        return mInstance;
    }

    /**
     * 获取可读数据库
     *
     * @return
     */
    private SQLiteDatabase getReadableDatabase() {
        if (mOpenHelper == null) {
            mOpenHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = mOpenHelper.getReadableDatabase();
        return db;
    }

    /**
     * 获取可写数据库
     *
     * @return
     */
    private SQLiteDatabase getWritableDatabase() {
        if (mOpenHelper == null) {
            mOpenHelper = new DaoMaster.DevOpenHelper(mContext, dbName, null);
        }
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        return db;
    }

    /**
     * 插入一条记录
     *
     * @param foodBean
     */
    public void insertUser(FoodBean foodBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.insert(foodBean);
    }

    /**
     * 插入用户集合
     *
     * @param foodBeanList
     */
    public void insertUserList(List<FoodBean> foodBeanList) {
        if (TextUtils.isEmpty(foodBeanList.toString())) {
            return;
        }
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.insertInTx(foodBeanList);
    }

    /**
     * 删除一条记录
     *
     * @param foodBean
     */
    public void deleteUser(FoodBean foodBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.delete(foodBean);
    }

    /**
     * 清空所有记录
     */
    public void clearFoodBean() {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.deleteAll();
    }

    /**
     * 批量删除
     *
     * @param list
     */
    public void deleteUsers(List<FoodBean> list) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.deleteInTx(list);
    }


    /**
     * 更新一条记录
     *
     * @param foodBean
     */
    public void updateUser(FoodBean foodBean) {
        DaoMaster daoMaster = new DaoMaster(getWritableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        foodBeanDao.update(foodBean);
    }

    /**
     * 查询用户列表
     */
    public List<FoodBean> queryFoodList() {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        QueryBuilder<FoodBean> qb = foodBeanDao.queryBuilder();
        List<FoodBean> list = qb.list();
        return list;
    }

    /**
     * 根据条件查询用户列表
     */
    public List<FoodBean> queryUserList(int ParentId) {
        DaoMaster daoMaster = new DaoMaster(getReadableDatabase());
        DaoSession daoSession = daoMaster.newSession();
        FoodBeanDao foodBeanDao = daoSession.getFoodBeanDao();
        QueryBuilder<FoodBean> qb = foodBeanDao.queryBuilder();
        qb.where(FoodBeanDao.Properties.Name.eq(ParentId)).orderAsc(FoodBeanDao.Properties.Py);
        List<FoodBean> list = qb.list();
        return list;
    }



}

